package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Appointment;
import cn.wolfcode.mapper.AppointmentMapper;
import cn.wolfcode.qo.AppointmentQueryObject;
import cn.wolfcode.service.IAppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    @Transactional
    public void save(Appointment appointment) {

        String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        int random = new Random().nextInt(90000) + 10000;
        appointment.setAno(date + random);
        appointment.setCreateTime(new Date());

        appointment.setStatus(Appointment.STATUS_TYPE_PERFORM);

        appointmentMapper.insert(appointment);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        appointmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Appointment appointment) {
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Appointment> listAll() {
        return appointmentMapper.selectAll();
    }

    @Override
    public PageInfo<Appointment> query(AppointmentQueryObject qo) {
        // 告诉插件我们查询的是第几页，每页多少条
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "a.create_time desc");
        // 查询当前页的数据
        List<Appointment> appointments = appointmentMapper.selectForList(qo);
        return new PageInfo<>(appointments);
    }


    @Override
    public void updateStatus(Long id, int status) {
        appointmentMapper.updateStatus(id, status);
    }
}
